package com.wondertek.service.mapper.business;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wondertek.service.dto.app.req.AppDerReq;
import com.wondertek.service.dto.app.res.DeviceErrorRecordListRes;
import com.wondertek.service.dto.business.req.WebDerReq;
import com.wondertek.service.dto.business.res.DeviceErrorRecordExportRes;
import com.wondertek.service.entity.business.DeviceErrorRecordPo;
import com.wondertek.service.entity.business.DevicePo;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author wangyu
 */
public interface DeviceErrorRecordMapper extends BaseMapper<DeviceErrorRecordPo> {

    /**
     * app查询报警
     * @param req
     * @return
     */
    @Select("<script> select (select count(i.id) from ipc_accessory i where i.accessory_sn=der.accessory_sn) ipcCount,id,device_name,device_id,accessory_sn,type,alarm_hub_name,error_type,status,error_message,error_time,room_id,room_name,site_id,site_name, (select is_read from device_error_record_read derr where derr.der_id = der.id and derr.consumer_id = #{req.consumerId} ) as is_read from device_error_record der where 1=1 and exists( select 1 from rental_device rd left join device d on rd.device_id = d.id where rd.is_delete = 0 and rd.end_date &gt; now() and rd.start_date &lt; now() and d.sn_code = der.device_id and rd.consumer_id = #{req.consumerId} and der.error_time >= rd.start_date )  <if test='req.type != null'> and type = #{req.type} </if> <if test='req.type == null'> and type in(1,2,3,4) </if> <if test='req.errorType != null and req.errorType.size > 0 '>  and error_type in <foreach collection='req.errorType' index='index' item='item' open='(' separator=',' close=')'>#{item}</foreach> </if> <if test='req.siteId != null and req.siteId.size > 0 '> and site_id in <foreach collection='req.siteId' index='index' item='item' open='(' separator=',' close=')'>#{item}</foreach> </if> <if test='req.roomId != null and req.roomId != \"\"'> and room_id = #{req.roomId} </if>  <if test='req.startDate != null'> and error_time &gt;= #{req.startDate} </if> <if test='req.endDate != null'> and error_time  &lt;= #{req.endDate} </if> order by error_time desc </script> ")
    IPage<DeviceErrorRecordListRes> findRecordList(AppDerReq req,Page page);


    /**
     * web查询报警
     * @param req
     * @return
     */
    @Select("<script> select (select count(i.id) from ipc_accessory i where i.accessory_sn=der.accessory_sn) ipcCount,type,case device_type when 1 then 'IPC' when 2 then 'alarm hub' when 3 then 'other' when 4 then 'accessory' else '-' end as device_type,case accessory_type when 1 then 'Smoke detector' when 2 then 'Wireless keyfob' when 3 then 'External Siren' when 4 then 'Door Detector' when 5 then 'Door Detector Plus' when 6 then 'Pir Detertor' else '-' end as accessory_type,accessory_sn,id,device_name,device_id,alarm_hub_name,error_type,status,error_message,error_time,room_id,room_name,site_id,site_name,is_read from device_error_record der where 1=1 <if test='req.industryId != null and req.industryId != \"\"'> and exists (select 1 from room r where r.id = der.room_id  and r.industry_type = #{req.industryId}) </if> <if test ='req.userId != \"1\"'> and exists ( select 1 from user_site us where us.user_id = #{req.userId} and us.site_id = der.site_id ) </if> <if test='req.type != null'> and type = #{req.type} </if> <if test='req.type == null'> and type in(1,3,4) </if> <if test='req.deviceTypeId != null and req.deviceTypeId != \"\"'>  and exists(select 1 from device d where d.is_delete = 0 and d.sn_code = der.device_id  and d.device_type_id = #{req.deviceTypeId}) </if>  <if test='req.nameOrId != null and req.nameOrId !=\"\"'> and (device_id like concat('%',#{req.nameOrId},'%') or accessory_sn like concat('%',#{req.nameOrId},'%')) </if>  <if test='req.errorType != null and req.errorType.size > 0 '>  and error_type in <foreach collection='req.errorType' index='index' item='item' open='(' separator=',' close=')'>#{item}</foreach> </if> <if test='req.siteId != null and req.siteId.size > 0 '> and site_id in <foreach collection='req.siteId' index='index' item='item' open='(' separator=',' close=')'>#{item}</foreach> </if>  <if test='req.roomId != null and req.roomId != \"\"'> and room_id = #{req.roomId} </if>  <if test='req.startDate != null'> and error_time &gt;= #{req.startDate} </if> <if test='req.endDate != null'> and error_time  &lt;= #{req.endDate} </if> order by error_time desc </script> ")
    Page<DeviceErrorRecordListRes> findWebRecordList(WebDerReq req, Page page);

    @Select("<script> select case type when 1 then 'Alarm' when 2 then 'Error' when 3 then 'Events' when 4 then 'Notification' end as type,case device_type when 1 then 'IPC' when 2 then 'alarm hub' when 3 then 'other' when 4 then 'accessory' else '-' end as device_type,case accessory_type when 1 then 'Smoke detector' when 2 then 'Wireless keyfob' when 3 then 'External Siren' when 4 then 'Door Detector' when 5 then 'Door Detector Plus' when 6 then 'Pir Detertor' else '-' end as accessory_type,accessory_sn,id,device_name,device_id,alarm_hub_name,error_type,status,error_message,error_time,room_id,room_name,site_id,site_name,case is_read when 0 then 'Unread' when 1 then 'Read' end as is_read from device_error_record der where 1=1 <if test='req.ids != null and req.ids.size > 0'> and der.id in <foreach collection='req.ids' index='index' item='item' open='(' separator=',' close=')'>#{item}</foreach> </if> <if test='req.industryId != null and req.industryId != \"\"'> and exists (select 1 from room r where r.id = der.room_id  and r.industry_type = #{req.industryId}) </if> <if test ='req.userId != \"1\"'> and exists ( select 1 from user_site us where us.user_id = #{req.userId} and us.site_id = der.site_id ) </if> <if test='req.type != null'> and type = #{req.type} </if> <if test='req.type == null'> and type in(1,3,4) </if> <if test='req.deviceTypeId != null and req.deviceTypeId != \"\"'>  and exists(select 1 from device d where d.is_delete = 0 and d.sn_code = der.device_id  and d.device_type_id = #{req.deviceTypeId}) </if>  <if test='req.nameOrId != null and req.nameOrId !=\"\"'> and (device_id like concat('%',#{req.nameOrId},'%') or accessory_sn like concat('%',#{req.nameOrId},'%')) </if>  <if test='req.errorType != null and req.errorType.size > 0 '>  and error_type in <foreach collection='req.errorType' index='index' item='item' open='(' separator=',' close=')'>#{item}</foreach> </if> <if test='req.siteId != null and req.siteId.size > 0 '> and site_id in <foreach collection='req.siteId' index='index' item='item' open='(' separator=',' close=')'>#{item}</foreach> </if>  <if test='req.roomId != null and req.roomId != \"\"'> and room_id = #{req.roomId} </if>  <if test='req.startDate != null'> and error_time &gt;= #{req.startDate} </if> <if test='req.endDate != null'> and error_time  &lt;= #{req.endDate} </if> order by error_time desc </script> ")
    List<DeviceErrorRecordExportRes> findWebRecordListExport(@Param("req") WebDerReq req);

    /**
     * 根据id查询报警详情
     * @param id
     * @return
     */
    @Select("<script> select (select count(i.id) from ipc_accessory i where i.accessory_sn=der.accessory_sn) ipcCount,accessory_type,type,accessory_sn,id,alarm_hub_name,(select sn_code from device d where d.id = der.device_id) as device_id,device_name,error_type,status,room_id,room_name,site_id,site_name,error_time,fixing_time,fixing_staff_name from device_error_record der where id = #{id} </script> ")
    DeviceErrorRecordPo findRecordDetailsById(@Param("id") String id);


    @Select("<script> select d.id as device_id,device_name,s.id as site_id,s.site_name,rd.consumer_id  <if test='roomCode != null'> , (select r.room_name from room r where r.device_id = d.id and room_code = #{roomCode}) as room_name, (select r.id from room r where r.device_id = d.id and room_code = #{roomCode}) as room_id </if>  from device d left join site s on s.id = d.site_id left join rental_device rd on rd.device_id = d.id and rd.end_date > now() and rd.is_delete = 0 where d.is_delete = 0 and d.sn_code = #{snCode} </script>")
    DeviceErrorRecordPo rollBackFind(@Param("snCode") String snCode, @Param("roomCode") Integer roomCode);


    @Select("select d.sn_code as deviceSn,d.id as device_id,device_name,s.id as site_id,s.site_name,(select category from device_type_detail dtd where dtd.id = d.device_type_id) as device_type  from device d left join site s on s.id = d.site_id  where d.is_delete = 0 and d.sn_code = #{snCode} ")
    DeviceErrorRecordPo findDeviceBySnCode(@Param("snCode") String snCode);


    @Select("select d.sn_code as deviceSn,a.accessory_type,a.sn_code as accessory_sn,d.id as device_id,accessory_name as device_name,s.id as site_id,s.site_name,r.id as room_id,r.room_name,(select category from device_type_detail dtd where dtd.id = d.device_type_id) as device_type from accessory a left join room r on a.room_id = r.id left join device d on r.device_id = d.id left join site s on d.site_id = s.id where a.is_delete = 0  and a.sn_code = #{snCode}")
    DeviceErrorRecordPo findAccessoryBySnCode(@Param("snCode") String snCode);

    /**
     * 根据deviceId设备id查询租赁的用户id
     * @param deviceId
     * @return
     */
    @Select("<script> select consumer_id from rental_device where is_delete = 0 and end_date &gt; now() and start_date &lt; now() and device_id = #{deviceId} <if test='roomId != null and roomId !=\"\"'> and room_id = #{roomId} </if> group by consumer_id </script>")
    List<String> findConsumerIdByDeviceId(@Param("deviceId") String deviceId,@Param("roomId") String roomId);

    @Select("<script> select fcm_token from consumer where id in <foreach collection=\"consumerIds\" index=\"index\" item=\"item\" open=\"(\" separator=\",\" close=\")\">#{item}</foreach>  </script>")
    List<String> findCidByConsumerIds(@Param("consumerIds") List<String> consumerIds);

    @Insert("insert into device_error_record_app_push values(#{message})")
    void addAppPushMessage(@Param("message") String message);

    @Select("<script> select new_error_type from device_error_record_transition where error_type = #{errorType}  <if test='category != null and category !=\"\"'> and category = #{category} </if> <if test='accessoryType != null and accessoryType != \"\"'> and accessory_type = #{accessoryType} </if> </script>")
    String findNewErrorType(@Param("category") String category, @Param("accessoryType") String accessoryType,@Param("errorType") String errorType );

    @Select("select time_zone from time_zone_dict where area_desc = #{areaDesc}")
    String findTimeZoneDict(@Param("areaDesc") String areaDesc);
}
